<?php
/**
 * API, handeld de request af en stuurt hem door naar de juiste class :)
 */

session_start();
include_once "lib/Db.php";
include_once "lib/ENV.php";

ENV::$db = new Db('mysql:host=109.70.1.132;dbname=aware_mike', 'aware_mike', 'mike123!');

try {

    $params = array();
    $params['controller'] = 'no_controller';
    $params['action'] = 'no_action';

    $params = array_merge($params, $_REQUEST);

    $controller = ucfirst(strtolower($params['controller']));
    $action = strtolower($params['action']) . 'Action';

    if (file_exists("controllers/{$controller}.php") ) {
        include_once "controllers/{$controller}.php";
    } else {
        throw new Exception('Controller is invalid.');
    }

    $controller = new $controller($params);

    if (method_exists($controllekr, $action) === false ) {
        throw new Exception('Action is invalid.');
    }

    $result['success'] = true;
    $result['data'] = $controller->$action();

} catch (Exception $e) {
    $result = array();
    $result['success'] = false;
    $result['error'] = $e->getMessage();
}


echo json_encode($result);
die();